package com.demo.controller;


import com.alipay.api.AlipayApiException;
import com.demo.controller.from.MovieOrderFrom;
import com.demo.exetion.MainExection;
import com.demo.model.MovieOrder;
import com.demo.service.impl.MovieOrderServiceImpl;
import com.demo.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 杨森
 * @since 2022-04-07
 */
@RestController
@RequestMapping("/movieOrder")
@Api(value = "订单", tags = "订单")
public class MovieOrderController {

    @Resource
    private MovieOrderServiceImpl movieOrderService;

    @PostMapping("/addMovieOrder")
    @ApiOperation(value = "创建订单", notes = "创建订单")
    public Result<Boolean> addMovieOrder(@RequestBody MovieOrderFrom movieOrderFrom, BindingResult bindingResult) throws MainExection {
        if (bindingResult.hasErrors()) {
            throw new MainExection(500, "参数校验未通过");
        }
        Boolean aBoolean = movieOrderService.addMovieOrder(movieOrderFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @PostMapping("/payOrder")
    @ApiOperation(value = "支付订单", notes = "支付订单")
    @ApiImplicitParam(value = "鉴权token", name = "authentication_token", paramType = "header", dataType = "String", required = true)
        public String payOrder(BigInteger orderid) throws MainExection, AlipayApiException {
        String payOrder = movieOrderService.payOrder(orderid);
        return payOrder;
    }

    @PostMapping("/notify")
    @ApiOperation(value = "支付宝回调", notes = "支付宝回调")
    public void notify(ServletRequest request, ServletResponse response) throws MainExection, AlipayApiException, UnsupportedEncodingException {
        movieOrderService.notify(request, response);
    }


    @PostMapping("/refund")
    @ApiOperation(value = "支付宝退款", notes = "支付宝退款")
    @ApiImplicitParam(value = "鉴权token", name = "authentication_token", paramType = "header", dataType = "String", required = true)
    public String refund(BigInteger orderid) throws MainExection, AlipayApiException, UnsupportedEncodingException {
        String refund = movieOrderService.refund(orderid);
        return refund;
    }

    @PostMapping("/queryOrder")
    @ApiOperation(value = "支付宝查询订单状态", notes = "支付宝查询订单状态")
    @ApiImplicitParam(value = "鉴权token", name = "authentication_token", paramType = "header", dataType = "String", required = true)
    public String queryOrder(BigInteger orderid) throws MainExection, AlipayApiException, UnsupportedEncodingException {
        String refund = movieOrderService.queryOrder(orderid);
        return refund;
    }

    @PostMapping("/OrderScore")
    @ApiOperation(value = "评分", notes = "评分")
    @ApiImplicitParam(value = "鉴权token", name = "authentication_token", paramType = "header", dataType = "String", required = true)
    public Result<Boolean> OrderScore(BigInteger orderid, Integer score) throws MainExection, AlipayApiException, UnsupportedEncodingException {
        Boolean aBoolean = movieOrderService.OrderScore(orderid, score);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @PostMapping("/iphoneMa")
    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    public Result<String> iphoneMa(String iphone) throws MainExection {
        String s = movieOrderService.iphoneMa(iphone);
        return Result.getInstance(String.class).setData(s);
    }

    @GetMapping("/getOrder")
    @ApiOperation(value = "获取订单详情", notes = "获取订单详情")
    @ApiImplicitParam(value = "鉴权token", name = "authentication_token", paramType = "header", dataType = "String", required = true)
    public Result<MovieOrder> getOrder(BigInteger orderid) throws MainExection {
        MovieOrder order = movieOrderService.getOrder(orderid);
        return Result.getInstance(MovieOrder.class).setData(order);
    }
}

